package com.huanying.goodjob.lottery;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huanying.framework.BaseDao;
import com.huanying.framework.PageBean;
import com.huanying.goodjob.prize_chance.Prize_chance;

@Service
public class LotteryServiceImpl implements LotteryService{

	@Autowired
	private BaseDao dao;
	@Override
	public void add_lottery(Lottery lottery) throws Exception {
		// TODO Auto-generated method stub
		dao.save(lottery);
	}

	@Override
	public void update_lottery(Lottery lottery) throws Exception {
		// TODO Auto-generated method stub
		dao.update(lottery);
	}

	@Override
	public void delete_lottery(int id) throws Exception {
		// TODO Auto-generated method stub
		String hql="update Lottery set status=0 where id=?";
		List<Object> param=new ArrayList<Object>();
		param.add(id);
	    dao.exculeByParams(hql, param);
	}

	@Override
	public Lottery get_lottery(int id) throws Exception {
		// TODO Auto-generated method stub
		return (Lottery)dao.load(Lottery.class, id);
	}

	@Override
	public PageBean search_lottery(Map<String, Object> map, int pageSize, int page) throws Exception {
		// TODO Auto-generated method stub
		StringBuffer hql=new StringBuffer("from Lottery as l where 1=1");
		List<Object> param = new ArrayList<Object>();
		if(map.get("name")!=null && map.get("name")!=""){
			hql.append(" and l.user.name like ?");
			param.add("%"+map.get("name")+"%");
		}
		if(map.get("day")!=null && map.get("day")!=""){
			hql.append(" and TIMESTAMPDIFF(DAY,DATE_FORMAT(l.create_time,'%Y-%m-%d'),DATE_FORMAT(NOW(),'%Y-%m-%d'))<=? ");
			param.add(Integer.parseInt((String) map.get("day")));
		}
		int allRow = dao.queryAllRowCount(hql.toString(), param);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		final int offset = PageBean.countOffset(pageSize, page);
		final int currentPage = PageBean.countCurrentPage(page);
		List<Object> list = dao.queryForPageAndParams(hql.toString(), param, offset, pageSize);

		// 把分页信息保存到Bean中
		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();

		return pageBean;
	}

	@Override
	public void update_lotterystatus(int id, int status) throws Exception {
		// TODO Auto-generated method stub
		String hql="update Lottery set status=? where id=?";
		List<Object> param=new ArrayList<Object>();
		param.add(status);
		param.add(id);
	    dao.exculeByParams(hql, param);
	}

	@Override
	public Prize_chance check_chance(int user_id) throws Exception {
		// TODO Auto-generated method stub
		String hql="from Prize_chance as p where p.user.id=? and p.chance_status=0 and p.status=1 order by p.id desc";
		List<Object> param=new ArrayList<Object>();
		param.add(user_id);
		Prize_chance prize_chance=null;
		if(dao.exculeHqlByParams(hql, param).size()>0)
		{
			prize_chance=(Prize_chance)dao.exculeHqlByParams(hql, param).get(0);
		}
		return prize_chance;
	}

	@Override
	public List<Object> front_get_lotterys(int user_id) throws Exception {
		// TODO Auto-generated method stub
		String hql="from Lottery as l where l.user.id=? and status=1";
		List<Object> param=new ArrayList<Object>();
		param.add(user_id);
		return (List<Object>)dao.exculeHqlByParams(hql, param);
	}

}
